package com.example.client_group5;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import util.HttpClientFactory;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

public class SignUpActivity extends Activity {
	private EditText editTextFullname;
	private String fullname = "";
	private EditText editTextEmail;
	private String mail = "";
	private EditText editTextUser;
	private String username = "";
	private EditText editTextPass;
	private String password = "";
	private View signUpButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//				WindowManager.LayoutParams.FLAG_FULLSCREEN);
//		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.sign_up);
        editTextFullname = (EditText) findViewById(R.id.fullname);
        editTextEmail = (EditText)findViewById(R.id.email);
        editTextUser = (EditText)findViewById(R.id.username_signin);
        editTextPass = (EditText)findViewById(R.id.password_signin);
        signUpButton = (View)findViewById(R.id.btRegister);
        
        enableButton();
        
        editTextFullname.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				enableButton();
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
			}
		});
        
        editTextEmail.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				enableButton();
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				
			}
		});
        
        editTextUser.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				enableButton();
				
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				
			}
		});
        editTextPass.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				enableButton();
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				
			}
		});
        
        signUpButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				fullname = editTextFullname.getText().toString();
				mail = editTextEmail.getText().toString();
				username = editTextUser.getText().toString();
				password = editTextPass.getText().toString();
				postData();
			}
		});
    }
    public void postData(){
    	try{
    		//HttpClient hc=new DefaultHttpClient();  
    		ResponseHandler <String> res=new BasicResponseHandler();  
    		
    		String postUrl = Setting.ipServer + "api/user/register";
    		
    		
    		HttpPost postMethod=new HttpPost(postUrl);  
    		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);    
    		Log.d("tag", "fullname / mail / username / password: "+ fullname + "	" + mail + "	"+  username + "	" + password);
    		nameValuePairs.add(new BasicNameValuePair("fullname", fullname.trim()));    
    		nameValuePairs.add(new BasicNameValuePair("email",  mail.trim())); 
    		nameValuePairs.add(new BasicNameValuePair("username", username.trim()));    
    		nameValuePairs.add(new BasicNameValuePair("password", password.trim()));    
    		postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
    		
    		//String response = hc.execute(postMethod,res); 
    		HttpClient client=HttpClientFactory.getThreadSafeClient();
    		String response = client.execute(postMethod,res); 
    		Log.d("AAA", "TRA VE: " + response);
    		
    		
    		JSONObject jsonObject = null;
    		jsonObject = new JSONObject(response);
    		Object result = jsonObject.get("isRegister");
			Log.d("tag", "Result: " + result.toString());
			if(result.toString().equals("true")){
				
				
				Intent i = new Intent(SignUpActivity.this, SignInActivity.class);
				startActivity(i);
				finish();
			}
			else{
				Object message = jsonObject.get("messageErrors");
				
				JSONArray arr = (JSONArray)message;
				Log.d("tag", "FFFFF: " + arr.toString());
				for(int i = 0; i < arr.length(); i ++){
					Log.d("tag", "AAAAA: " + arr.getString(i));
					Toast toast = Toast.makeText(SignUpActivity.this,arr.getString(i) , Toast.LENGTH_LONG);
					toast.show();
				}
			}
    		
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }

    private void enableButton(){
    	if("".equalsIgnoreCase(editTextEmail.getText().toString().trim())||
    		"".equalsIgnoreCase(editTextFullname.getText().toString().trim()) ||
    		"".equalsIgnoreCase(editTextPass.getText().toString().trim()) ||
    		"".equalsIgnoreCase(editTextUser.getText().toString().trim())){
    		signUpButton.setEnabled(false);
    		signUpButton.setBackgroundResource(R.drawable.bg_button_sign);
    		return;
    	}
    	signUpButton.setEnabled(true);
    	signUpButton.setBackgroundResource(R.drawable.bg_signin_yellow);
    	
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent i = new Intent(getApplicationContext(), SignInActivity.class);
			startActivity(i);
			finish();
		}
		return false;

	}

	public void onDestroy() {
		System.gc();
		super.onDestroy();
	}
   
}
